package com.cinema.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 订单工具类
 */
public class OrderUtil {
    
    private static final Random random = new Random();
    
    /**
     * 生成订单号
     * 格式：手机号后4位 + 年月日 + 时分秒 + 3位随机数
     */
    public static String generateOrderNo(String phone) {
        StringBuilder orderNo = new StringBuilder();
        
        // 手机号后4位
        if (phone != null && phone.length() >= 4) {
            orderNo.append(phone.substring(phone.length() - 4));
        } else {
            orderNo.append("0000");
        }
        
        // 当前时间：年月日时分秒
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        orderNo.append(sdf.format(new Date()));
        
        // 3位随机数
        orderNo.append(String.format("%03d", random.nextInt(1000)));
        
        return orderNo.toString();
    }
    
    /**
     * 计算订单过期时间
     */
    public static Date calculateExpireTime(int expireMinutes) {
        return new Date(System.currentTimeMillis() + expireMinutes * 60 * 1000L);
    }
    
    /**
     * 检查订单是否过期
     */
    public static boolean isOrderExpired(Date expireTime) {
        return expireTime != null && new Date().after(expireTime);
    }
    
    /**
     * 获取订单状态显示文本
     */
    public static String getOrderStatusText(String status) {
        if (status == null) return "未知";
        
        switch (status) {
            case "PENDING":
                return "待支付";
            case "PAID":
                return "已支付";
            case "CANCELLED":
                return "已取消";
            case "REFUNDED":
                return "已退款";
            case "EXPIRED":
                return "已过期";
            default:
                return status;
        }
    }
    
    /**
     * 获取订单状态样式类
     */
    public static String getOrderStatusClass(String status) {
        if (status == null) return "secondary";
        
        switch (status) {
            case "PENDING":
                return "warning";
            case "PAID":
                return "success";
            case "CANCELLED":
                return "secondary";
            case "REFUNDED":
                return "info";
            case "EXPIRED":
                return "danger";
            default:
                return "secondary";
        }
    }
}
